import scrapy

from facebook_crawler.items import FacebookPostItem


class FacebookPostSpider(scrapy.Spider):
    name = "facebook_post"
    start_urls = [
        'http://quotes.toscrape.com/page/1/',
    ]

    def parse(self, response):
        item = FacebookPostItem()
        for quote in response.css('div.quote'):
            item['post_user_name'] = quote.css('small.author::text').get()
            item['post_content'] = quote.css('span.text::text').get()
            yield item
            # yield {
            #     'text': quote.css('span.text::text').get(),
            #     'author': quote.css('small.author::text').get(),
            #     'tags': quote.css('div.tags a.tag::text').getall(),
            # }

        next_page = response.css('li.next a::attr(href)').get()
        if next_page is not None:
            next_page = response.urljoin(next_page)
            yield scrapy.Request(next_page, callback=self.parse)
